package com.zues.thread.lock.dead.v1;

import com.zues.thread.util.ThreadUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 哲学家类
 *
 * @author zues
 * @date 2/17/2025 9:04 下午
 */
@Slf4j(topic = "c.Philosopher")
public class Philosopher extends Thread{

    Chopstick left;
    Chopstick right;

    public Philosopher(String name, Chopstick left, Chopstick right) {
        super(name);
        this.left = left;
        this.right = right;
    }

    @Override
    public void run() {
        while (true){
            //尝试获取左手筷子
            synchronized (left){
                //尝试获取右手筷子
                synchronized (right){
                    // 哲学家吃饭
                    eat();
                }
            }

        }
    }

    private void eat() {
        log.debug("eating...");
        //等待相当于思考
        ThreadUtil.sleep(1);
    }
}
